package com.ruoyi.project.data.dataManager.service.impl;
import com.ruoyi.project.data.config.domain.DsImportConfig;
import com.ruoyi.project.data.config.service.IDsImportConfigService;
import com.ruoyi.project.data.dataManager.domain.DataParamVo;
import com.ruoyi.project.data.dataManager.domain.SocketClient;
import com.ruoyi.project.data.dataManager.service.IDataManagerService;
import com.ruoyi.project.tool.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * 交易流水Service业务层处理
 *
 * @author 关晓强
 * @date 2020-03-16
 */
@Service
public class DateManagerServiceImpl implements IDataManagerService
{
    @Autowired
    private IDsImportConfigService dsImportConfigService;

    @Override
    public boolean start(DataParamVo vo) throws Exception {
        String msgType = vo.getMsgType();
        String dataType = vo.getDataType();
        String dateDate = null;
        if (msgType.equalsIgnoreCase("restart")
                || msgType.equalsIgnoreCase("timerend")) {
            dateDate = DateTimeUtil.getDate();
        } else {
            dateDate = vo.getDateDate();
        }
        // 20090909转换为2009-09-09格式
        if (dateDate != null && dateDate.length() == 8) {
            dateDate = dateDate.substring(0, 4) + "-"
                    + dateDate.substring(4, 6) + "-" + dateDate.substring(6, 8);
        }
        Map<String, Object> transportInfo = new HashMap<String, Object>();
        transportInfo.put("dataType", dataType);
        transportInfo.put("message", msgType);
        transportInfo.put("trdate", dateDate);
        transportInfo.put("IP", InetAddress.getLocalHost().toString());
        transportInfo.put("dataDate", dateDate);
        String configId = "";
        if("ocr".equals(dataType)){
            configId = "1";
        }else if("imp".equals(dataType)){
            configId = "2";
        }else if("bms".equals(dataType)){
            configId = "3";
        }
        DsImportConfig importConfig = dsImportConfigService.selectDsImportConfigByCode(configId);
        if(importConfig == null) {
            throw new RuntimeException("未配置ip地址和端口");
        }
        SocketClient client = new SocketClient(transportInfo, importConfig.getSocketip(), importConfig.getSocketport());
        client.transport();
        //增加系统日志
        /*User user = ShiroUtils.getSysUser();
        java.sql.Date date=new java.sql.Date(System.currentTimeMillis());

        String strContent= "操作员 "+user.getUserId()+" 于  "+date.toString()+"手工导入  "+"名称"+dataType+"交易日期"+dateDate+" 。";

        try {
            serviceSmOperatorlogImpl.saveSysLog(session,request,"1",user,"手工导入 表",strContent);
        } catch (Exception e) {

        }*/
        return true;
    }
}
